package com.tzx.fluttermixedapplication.activity

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.LinearLayout
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.tzx.fluttermixedapplication.R
import io.flutter.embedding.android.FlutterFragmentActivity
import kotlinx.coroutines.launch

class LifeCycleTestActivity : ComponentActivity() {
    private lateinit var container: LinearLayout
    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_layout)
        Log.d("tanzhenxing", "onCreate")
        lifecycleScope.launch {
            Log.d("tanzhenxing", "call repeatOnLifecycle start")
            repeatOnLifecycle(Lifecycle.State.CREATED) {
                Log.d("tanzhenxing", "Lifecycle.State.STARTED")
            }
            // DESTROYED状态的时候才会执行这部分代码
            Log.d("tanzhenxing", "call repeatOnLifecycle end")
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d("tanzhenxing", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("tanzhenxing", "onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("tanzhenxing", "onResume")
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                // 一开始这里就会接受到CREATED\STARTED\RESUMED三种状态
                Log.d("tanzhenxing", "onStateChanged:${event.targetState.name}")
            }
        })
    }

    override fun onPause() {
        super.onPause()
        Log.d("tanzhenxing", "onPause")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("tanzhenxing", "onDestroy")
    }
}